package com.tjn.generic;

import java.util.ArrayList;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class Generic01 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        //创建一个ArrayList对象
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("旺财",1));
        arrayList.add(new Dog("发财",2));
        arrayList.add(new Dog("小黄",3));

        //现在要通过getter获取对象的属性
        //按照传统方法遍历得到
        //按传统方法要强转成Dog对象才能调用getter方法，如果我现在要添加一个cat对象，就会报异常
        //为了解决这个问题，就引入了泛型

        arrayList.add(new Cat("招财",4));
        //在这里添加cat对象之后，在遍历的时候，就会报异常，引出泛型可以解决这两个问题

        for (Object o : arrayList) {
            Dog dog = (Dog) o;
            System.out.println(dog.getName() + " " + dog.getAge());
        }

    }
}
class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
}